// pages/collectSchool/collectSchool.js
import { file } from '../../server/file';
import { school } from '../../server/school';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        schools: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCollectSchool();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    /*
     * 返回上一页
     */
    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },

    getCollectSchool() {
        school.getCollectSchool().then(async (res) => {
            console.log(res);
            let schools = [];
            for (let i = 0; i < res.data.collectSchoolList.length; i++) {
                let item = res.data.collectSchoolList[i];
                let imgUrl = await file.downloadPublicFile(item.iconId);
                imgUrl = imgUrl.data.url;
                let tmp = {
                    id: item.id,
                    iconUrl: imgUrl,
                    name: item.name,
                    majorCount: item.majorCount,
                    studentCount: item.studentCount
                };
                schools.push(tmp);
            }
            this.setData({ schools });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },
    openSchoolInfo(e) {
        const schoolId = e.currentTarget.dataset.school.id;
        wx.navigateTo({
          url: `/pages/schoolInfo/schoolInfo?schoolId=${schoolId}`,
        });
    }
})